﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using moc.BLL;
using moc.IBLL;
using moc.DAL;
using moc.IDAL;
using moc.Models;

namespace moc.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            AddDependencies(services);
            //创建的服务实例由自己保存，所以同一个对象提供的服务实例均是同一个对象
            //报证db上下文实例唯一
            services.AddEntityFrameworkMySql().AddDbContext<MocDbContext>(ServiceLifetime.Scoped);

           


            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
        //依赖注入
        public IServiceCollection AddDependencies(IServiceCollection services)
        {
            services.AddScoped<IUnitOfWork, UnitOfWork<MocDbContext>>();
            //第一个泛型类型表示将要从容器中请求的类型（通常是一个接口）
            //第二个泛型类型表示将由容器实例化并且用于完成这些请求的具体类型。
            services.AddScoped<ICustomerLoginDal, CustomerLoginDal>();
            services.AddScoped<ICustomerLoginService, CustomerLoginService>();                       
            return services;
        }
    }
}
